package com.fifth.home.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Banner;
import com.fifth.common.pojo.entity.BottomArticle;
import com.fifth.common.pojo.entity.Link;
import com.fifth.common.pojo.entity.Zone;
import com.fifth.home.mapper.LinkMapper;
import com.fifth.home.service.ILinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 第五组
 * @since 2022-09-01
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements ILinkService {
    @Resource
    private LinkMapper linkMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public CommonResult selectAllLink(Integer currentPage, Integer pageSize, Link link) {
        IPage<Link> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(link.getLinkName()),"link_name",link.getLinkName());
        wrapper.eq(StringUtils.isNotBlank(link.getIsDisable()),"is_disable",link.getIsDisable());
        wrapper.eq("is_deleted","0");
        wrapper.orderByAsc("link_id");

        IPage<Link> zoneIPage = linkMapper.selectPage(page, wrapper);

        return new CommonResult(2000,"查询友情链接成功",zoneIPage);
    }

    /**
     * 后台添加友情链接
     * @param link
     * @return
     */
    @Override
    public CommonResult insertLink(Link link) {
        int insert = linkMapper.insert(link);

        String link1 = redisTemplate.opsForValue().get("link");
        if(link1!=null && !"".equals(link1)){
            Boolean head1 = redisTemplate.delete("link");
        }
        return new CommonResult(2000,"添加友情链接成功",null);
    }

    /**
     * 修改友情链接状态
     * @param linkId
     * @param isDisable
     * @return
     */
    @Override
    public CommonResult updateStatus(String linkId, String isDisable) {
        QueryWrapper<Link> wrapper = new QueryWrapper<>();
        wrapper.eq("link_id", linkId);
        Link link = new Link();
        link.setIsDisable(isDisable);
        int update = linkMapper.update(link, wrapper);

        String link1 = redisTemplate.opsForValue().get("link");
        if(link1!=null && !"".equals(link1)){
            //删除redis数据
            Boolean head1 = redisTemplate.delete("link");
        }
        return new CommonResult(2000, "修改状态成功", null);
    }

    /**
     * 删除友情链接
     * @param linkId
     * @return
     */
    @Override
    public CommonResult removeLink(String linkId) {
        int i = linkMapper.deleteById(linkId);

        String link = redisTemplate.opsForValue().get("link");
        if(link!=null && !"".equals(link)){
            Boolean bottomArticle1 = redisTemplate.delete("link");
        }
        return new CommonResult(2000, "删除友情链接成功", null);
    }

    /**
     * 修改友情链接
     * @param link
     * @return
     */
    @Override
    public CommonResult updateLink(Link link) {
        link.setGmtModified(null);
        int update = linkMapper.updateById(link);

        String link1 = redisTemplate.opsForValue().get("link");
        if(link1!=null && !"".equals(link1)){
            Boolean banner2 = redisTemplate.delete("link");
        }
        return new CommonResult(2000, "修改友情链接成功", null);
    }

    /**
     * 查询前端友情链接
     * @return
     */
    @Override
    public CommonResult selectQianAllLink() {
        if (redisTemplate.hasKey("link")) {
            //查看redis是否存在prefecture
            List<Link> list = JSON.parseObject(redisTemplate.opsForValue().get("link"), List.class);
            return new CommonResult(2000, "轮播查询成功", list);

        } else {
            QueryWrapper<Link> wrapper = new QueryWrapper<>();
            wrapper.eq("is_disable", 0);
            wrapper.eq("is_deleted", 0);
            wrapper.orderByAsc("link_id");
            List<Link> list = linkMapper.selectList(wrapper);

            String s = JSON.toJSONString(list);
            //存入redis
            redisTemplate.opsForValue().set("link", s,24, TimeUnit.HOURS);
            return new CommonResult(2000, "友情链接查询成功", list);
        }
    }


}
